package cn.edu.cque.mall.controller;

import cn.edu.cque.mall.entity.Category;
import cn.edu.cque.mall.entity.PageResult;
import cn.edu.cque.mall.entity.Product;
import cn.edu.cque.mall.service.CategoryService;
import cn.edu.cque.mall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * @ClassName ProductController
 * @Description
 * @Author chen
 * @Date 2020/10/11 0011 22:08
 */

@Controller
@RequestMapping("/product")
public class ProductController {
    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;

    @RequestMapping("/detail")
    public ModelAndView findById(String id) {
        ModelAndView modelAndView = new ModelAndView();
        Product product = productService.findById(id);
        modelAndView.addObject("product", product);
        modelAndView.setViewName("/jsp/detail");
        return modelAndView;
    }


    @RequestMapping("/list")
    public ModelAndView findListByCid(String cid, String currentPage, String pageSize) {
        ModelAndView modelAndView = new ModelAndView();

        PageResult<Product> pageResult = productService.findPageResultByCid(cid, Integer.parseInt(currentPage), Integer.parseInt(pageSize));
        Category category = categoryService.findById(cid);

        modelAndView.addObject("category", category);
        modelAndView.addObject("pageResult", pageResult);

        modelAndView.setViewName("/jsp/list");

        return modelAndView;
    }

}
